package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShBackUserDao;
import com.shop.shoppingmall.dao.ShMerchantDao;
import com.shop.shoppingmall.entity.ShBackUser;
import com.shop.shoppingmall.entity.ShMerchant;
import com.shop.shoppingmall.service.ShBackUserService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.PhotoUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import net.sf.saxon.expr.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (ShBackUser)表控制层
 *
 * @author makejava
 * @since 2020-09-16 16:45:20
 */
@RestController
@RequestMapping("shBackUser")
@CrossOrigin
public class ShBackUserController {
    /**
     * 服务对象
     */
    @Resource
    private ShBackUserService shBackUserService;
    @Resource
    private ShBackUserDao shBackUserDao;
    @Resource
    private ShMerchantDao shMerchantDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShBackUser selectOne(String id) {
        return this.shBackUserService.queryById(id);
    }

    /**
     * 后台用户注册
     * @param jsonObject
     * @return
     */
    @PostMapping("/backRegister")
    public Result backRegister(@RequestBody JSONObject jsonObject){
        return shBackUserService.backRegister(jsonObject);
    }

    /**
     * 后台用户登录
     * @param map
     * @return
     */
    @PostMapping("/userLogin")
    public Result userLogin(@RequestBody Map map){
        return shBackUserService.userLogin(map);
    }

    /**
     * 查询首页所有相关信息
     * @return
     */
    @GetMapping("/selectHomeAll")
    public Result selectHomeAll(){
        return shBackUserService.selectHomeAll();
    }

    /**
     * 用户修改个人信息
     * @param shBackUser
     * @param
     * @return
     * @throws IOException
     */
    @PostMapping("/updateShUserByUserId")
    public Result updateShUserByUserId(@RequestBody ShBackUser shBackUser){
        return shBackUserService.updateShUserByUserId(shBackUser);
    }

    /**
     * 查询使用后台用户的信息
     * @param map
     * @return
     */
    @PostMapping("/selectQueryAll")
    public Result selectQueryAll(@RequestBody Map map){
        return shBackUserService.selectQueryAll(map);
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody Map map,HttpServletRequest request){
        String oldpassword = (String) map.get("oldpassword");
        String newpassword = (String) map.get("newpassword");
        String confirmpassword = (String) map.get("confirmpassword");
        return shBackUserService.resetPassword(request,oldpassword,newpassword,confirmpassword);
    }

    /**
     * 重置密码
     * @param map
     * @return
     */
    @PostMapping("/chongZhiPassword")
    public Result chongZhiPassword(@RequestBody Map map){
        String phone = (String) map.get("phone");
        String smscode = (String) map.get("smscode");
        String password = (String) map.get("password");
        String loginName = (String) map.get("loginName");
        return shBackUserService.chongZhiPassword(phone,loginName,smscode,password);
    }

    /**
     * 用于数据回显
     * @param map
     * @return
     */
    @PostMapping("/selectByUserId")
    public Result selectByUserId(@RequestBody Map map){
        String userId = (String) map.get("userId");
        return shBackUserService.selectByUserId(userId);
    }

    /**
     * 管理系统查询用户注册量和订单量
     * @param request
     * @return
     */
    @PostMapping("/queryCount")
    public Result queryCount(HttpServletRequest request){
        String name = JwtUtil.getUserNameByToken(request);
        ShBackUser shBackUser = shBackUserDao.selectLoginName(name);
        if (shBackUser == null || name == null) {
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String merchantId="";
        List<ShMerchant> shMerchants = shMerchantDao.selectByUserId(shBackUser.getUserId());
        if(shMerchants.size()>0){
            return shBackUserService.queryCount(shMerchants.get(0).getId());
        }
        return shBackUserService.queryCount(merchantId);
    }
    /**
     * 管理系统查询各个状态订单百分比
     * @param request
     * @return
     */
    @PostMapping("/selectOrderS")
    public Result selectOrderS(HttpServletRequest request){
        String name = JwtUtil.getUserNameByToken(request);
        ShBackUser shBackUser = shBackUserDao.selectLoginName(name);
        if (shBackUser == null || name == null) {
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String merchantId="";
        List<ShMerchant> shMerchants = shMerchantDao.selectByUserId(shBackUser.getUserId());
        if(shMerchants.size()>0){
            return shBackUserService.selectOrderS(shMerchants.get(0).getId());
        }
        return shBackUserService.selectOrderS(merchantId);
    }
}
